
var util = require('../../utils/util.js');
var utilMd5 = require('../../utils/md5.js');
var app = getApp();
var wxAuthFail = true; 


Page({
  data: {
    util: util,
    tc: 0,

    isIphone_x: false,
    nav_bgColor: '#ffffff',
    
    version: util.version,
    token: '',
    uid: 0,
    userinfo:{},


    isShow: false,

    user_jifen: 0,

    weekArray:[], 

  
  },

  isLogin: false,

  onLoad: function(data){
    
    var that = this;
    var tc = app.get_tc();//简体繁体转换
    this.setData({
      tc: tc,
    });

    this.getLoginInfo();
    
    //获取颜色配置
    app.getColorSetData(function (color) {
      that.setData({
        colorSet: color,
      })
      that.resetNavColor();
    });


    wx.getSystemInfo({
      success: function(res) {
        var isIphone_x = (res.model.indexOf('iPhone X') >= 0);
        that.setData({
          isIphone_x: isIphone_x,
        });
      }
    });
  },

  onShow:function(){
    var membercard = wx.getStorageSync('membercard');
    if (membercard) {
      this.isLogin = true;
    }
  },


   //时间戳转换时间
    //时间戳转换时间  
  toDate:function(number){  
      var n=number * 1000;  
      var date = new Date(n);  
      var Y = date.getFullYear() + '-';  
      var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';  
      var D = date.getDate()<10 ? '0'+date.getDate() : date.getDate();  
      return (Y+M+D)  
  },

 

  //获取用户这周的签到情况
  getWeekPoints:function(){
    var that = this;
    util.api({
      data: {
        "publickey": util.publiccode,
        "ac": "getqiandao",
        "uid": that.data.uid,
      },
      success: function (responseData) {
        console.log("getWeekPoints",responseData.data);
        wx.hideLoading();
        var result = responseData.data;
        if(result.res == false){

        }else{
          var date = new Date();
          var day = date.getDate();
          var todayTime = parseInt(date.getTime() / 1000);
          var todate = that.toDate(todayTime);
          for (var i = 0; i < result.length; i++) {
            
            if(result[i]["isqian"] == "0"){
              
              if(todate>result[i]["date"]){
                result[i]["res"] ="miss";
              }else{
                result[i]["res"] ="no";
              }
            }else{
              result[i]["res"] ="yes";
            }
            
          }

          that.setData({
            weekArray:result,
          })

        }
        
      }
    });
  },




  //签到
  qiandaotijiao:function(){
    var that = this;
    if(this.isLogin){
      var privatekey = utilMd5.hexMD5(that.data.uid + 'addqiandao' + that.data.token);
      var data = {
        ac: "addqiandao",
        privatekey: privatekey,
        uid: that.data.uid,
      };

      util.api({
        data: data,
        method: 'POST',
        success: function (responseData) {
          var resData = responseData.data;
          console.log(resData);
          if (resData.res == true) {
            wx.showToast({
              title: '簽到成功',
              icon: 'success',
              duration: 2000
            })
            that.getWeekPoints();
          } else {
            
            wx.showModal({
              title: '提示',
              content: that.data.tc == 1 ? "今日已簽到並獲得了1積分" : "今日已签到并获得了1积分",
              showCancel: false,
            })
          }

        }
      });
    } else {
      var membercard = this.data.userinfo.mobile;
      console.log(membercard);
      if (!membercard) {
        wx.navigateTo({
          url: '/pages/join/join?',
        })
      } else {
        wx.navigateTo({
          url: '/pages/login/login?membercard=' + membercard,
        })
      }
    }
  },

 


  getJifen: function () {
    var that = this;
    util.api({
      data: {
        "publickey": util.publiccode,
        "ac": "getusersource",
        "uid": that.data.uid
      },
      success: function (responseData) {
        that.setData({
          user_jifen: responseData.data.source
        })


        var titleName = that.data.tc == 1 ? '簽到 (現有積分'+responseData.data.source+')' : '签到 (现有积分'+responseData.data.source+')';
        
        wx.setNavigationBarTitle({
          title: titleName,
        })
      }
    });
  },

  getLoginInfo: function(){
    wx.showLoading({
      title: 'loading...',
    })
    var that = this;
    app.getLoginUserInfo(function (res) {
      console.log("mine login:", res);
      that.setData({
        uid: res.uid,
        token: res.token,
        userinfo: res
      });
      that.getJifen();

      that.getWeekPoints();
    });
  },



  //保存formId
  submitFormId: function (e) {
    console.log("保存formId！！！！！！！！！！", e.detail);
    app.saveFormId(e.detail.formId);

    var path = e.detail.value.nav_path;
    if (path && path.indexOf('navto:') == 0) {
      var navPath = path.slice(6);
      wx.navigateTo({
        url: navPath,
      })
    } else {
      wx.redirectTo({ url: path });
    }
  },





  resetNavColor: function () {
    var bg_color = '#d8202d';
    var fg_color = '#ffffff';
    if (this.data.colorSet) {
      var colorJO = this.data.colorSet;
      bg_color = colorJO.titlebg;
      fg_color = colorJO.titlefont;
    }
    wx.setNavigationBarColor({
      frontColor: fg_color,
      backgroundColor: bg_color
    })
  },

 



})